package com.ten.cotroller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.ten.pojo.ParkResult;
import com.ten.pojo.SpotCheckInfo;
import com.ten.pojo.SpotSalesInfo;
import com.ten.pojo.WebCheckInfo;
import com.ten.pojo.WebOrderInfo;
import com.ten.service.HttpMessage;

@Controller
public class ParkController {
	@Autowired
	private HttpMessage httpMessage;

	@RequestMapping("/searchSum")
	public String searchSum(String beginTime, String endTime, String parkFullName, HttpServletRequest req) {
		String msg = httpMessage.getSpotsSaleInfo(beginTime, endTime, parkFullName);
		String msgCheck = httpMessage.getSpotsCheckInfo(beginTime, endTime, parkFullName);
		if (!("[]".equals(msg)) && !("[]".equals(msgCheck))) {
			List<SpotSalesInfo> info = JSON.parseArray(msg, SpotSalesInfo.class);
			List<SpotCheckInfo> checks = JSON.parseArray(msgCheck, SpotCheckInfo.class);
			ParkResult result = new ParkResult();
			int sellNum = 0;
			int checkNum = 0;
			String parkName = info.get(0).getParkFullName();
			if (info != null && info.size() > 0) {
				for (SpotSalesInfo sale : info) {
					sellNum += Integer.parseInt(sale.getTicketNoCount());
				}
			}
			if (checks != null && checks.size() > 0) {
				for (SpotCheckInfo check : checks) {
					checkNum += Integer.parseInt(check.getAlreadyUseCount());
				}
			}
			if ("006".equals(parkFullName)) {
				String webOrderMsg = httpMessage.getWebOrderInfo(beginTime, endTime);
				String webCheckMsg = httpMessage.getWebCheckInfo(beginTime, endTime);
				List<WebOrderInfo> webOrders = JSON.parseArray(webOrderMsg, WebOrderInfo.class);
				List<WebCheckInfo> webChecks = JSON.parseArray(webCheckMsg, WebCheckInfo.class);
				if (webOrders != null && webOrders.size() > 0) {
					for (WebOrderInfo webOrder : webOrders) {
						sellNum += Integer.parseInt(webOrder.getTicketCount());
					}
				}
				if (webChecks != null && webChecks.size() > 0) {
					for (WebCheckInfo webCheck : webChecks) {
						checkNum += Integer.parseInt(webCheck.getCheckNum());
					}
				}
			}
			result.setCheckNum(checkNum);
			result.setParkFullName(parkName);
			result.setSellNum(sellNum);
			req.setAttribute("result", result);
		}
		return "detail";
	}
}
